/**
 * @Title: AppInstallUtils.java
 * @Package com.travelink.upgrade.utils
 * @Description: TODO(用一句话描述该文件做什么)
 * @author zhangzhl
 * @date 2017-7-27 下午3:29:52
 * @version V1.0
 */
package com.bruce.apkinstaller.other;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author zhangzhl
 * @ClassName: AppInstallUtils
 * @Description: TODO(app的安装 卸载)
 * @date 2017-7-27 下午3:29:52
 */
public class AppInstallUtils {

    /*
     * 安装apk
     */
    public static Object install(String apkAbsolutePath) {
        Exception exception = null;
        File file = new File(apkAbsolutePath);
        if (!file.exists()) {
            exception = new FileNotFoundException("文件不存在，请确认是否下载成功。");
            return exception;
        }
        String[] args = {"pm", "install", "-r", apkAbsolutePath};
        // String[] args = { "adb", "install", "-r", apkAbsolutePath };
        String result = "";
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Process process = null;
        InputStream errIs = null;
        InputStream inIs = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int read = -1;
            process = processBuilder.start();
            errIs = process.getErrorStream();
            while ((read = errIs.read()) != -1) {
                baos.write(read);
            }
            baos.write('\n');
            inIs = process.getInputStream();
            while ((read = inIs.read()) != -1) {
                baos.write(read);
            }
            byte[] data = baos.toByteArray();
            result = new String(data);
        } catch (IOException e) {
            e.printStackTrace();
            exception = e;
        } catch (Exception e) {
            e.printStackTrace();
            exception = e;
        } finally {
            try {
                if (errIs != null) {
                    errIs.close();
                }
                if (inIs != null) {
                    inIs.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (process != null) {
                process.destroy();
            }
        }

        if (result != null && (result.endsWith("Success") || result.endsWith("Success\n"))) {
            return true;
        }
        return exception;
    }

    /**
     * 卸载
     *
     * @param packageName
     */
    public static void uninstall(String packageName) {
        String[] args = {"pm", "uninstall", packageName};
        // String[] args = { "adb", "uninstall", packageName };
        String result = null;
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        ;
        Process process = null;
        InputStream errIs = null;
        InputStream inIs = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int read = -1;
            process = processBuilder.start();
            errIs = process.getErrorStream();
            while ((read = errIs.read()) != -1) {
                baos.write(read);
            }
            baos.write('\n');
            inIs = process.getInputStream();
            while ((read = inIs.read()) != -1) {
                baos.write(read);
            }
            byte[] data = baos.toByteArray();
            result = new String(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (errIs != null) {
                    errIs.close();
                }
                if (inIs != null) {
                    inIs.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (process != null) {
                process.destroy();
            }
        }
    }

}
